<?php
namespace Home\Controller;
use Think\Controller,
    Model\News_classModel,
    Model\News_contentsModel;

class NewslistController extends Controller {
    
    function news(){
        $ncmd = new News_classModel();
        $ntmd = new News_contentsModel();
        
        //获取升序第二的栏目
        $news_class = $ncmd -> where('state=0') -> order('sort asc') -> select();
             
        //获得栏目的ID
        $id   = empty($_GET['id'])?$news_class[0]['id']:$_GET['id'];
        
        //栏目名称
        $name = $ncmd -> find($id);
        
        //获得栏目内容列表，分页
        //总条数
        $total = $ntmd -> where("n_id='{$id}' AND state=1") -> count();
        //每页显示条数
        $rows = 3;
        //实例化
        $page = new \Tools\Page($total,$rows);
        $limit = $page -> getLimit();
        $list  = $ntmd -> where("n_id='{$id}' AND state=1") -> limit($limit) -> order('sort desc') -> select();
        $fpage = $page -> fpage(array(2,3,4,5,6));
               
         //轮播图
        $round  = $ntmd -> where('news_round=1 AND state=1')-> order('sort desc')  -> find();
        $roundB = $ntmd -> where('news_round=1 AND state=1')-> order('sort desc')  -> limit(1,5) -> select();

        $this -> assign('round',$round);      
        $this -> assign('roundB',$roundB); 
        $this -> assign('list',$list);
        $this -> assign('page',$fpage);
        $this -> assign('name',$name);
        $this -> assign('news_class',$news_class);
        $this -> display();
    }
    
    
    function show($id){
        $ntmd = new News_contentsModel();
           
        $news = $ntmd -> find($id);
        
        //同一电脑一小时内只能加一次点击数
        if(!$_SERVER['REMOTE_ADDR'] == cookie('ip'.$id)){
            $jia['id'] = $id;
            $jia['click'] = $news['click']+1;
            $ntmd -> save($jia);
            cookie('ip'.$id,$_SERVER['REMOTE_ADDR'],3600);
        }
        
        $news = $ntmd -> find($id);    
        
        $this -> assign('news',$news);
        $this -> display();
    }
   
}